﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaseCoder.Utility;
using System.IO;
using System.Xml.Linq;

namespace BaseCoder.ManagePageCoder
{
    public class ManageSettings
    {
        public static void Create(Table table, TableColumn[] cols)
        {
            if (string.IsNullOrEmpty(AppConfig.ManageSettingsRoot)) return;
            if (cols == null || cols.Length <= 0) return;
            if (File.Exists(AppConfig.ManageSettingsRoot + table.Name + ".xml"))
            {
                Console.WriteLine("{0} 已存在，跳过配置文件的生成。", AppConfig.ManageSettingsRoot + table.Name + ".xml");
                //return;
            }
            XDocument document = new XDocument(
                new XDeclaration("1.0", "utf-8", ""));
            XElement root = new XElement("setting");
            root.Add(new XElement("tableName", table.Name));
            root.Add(new XElement("description", table.Description));
            root.Add(new XElement("requiredMgr", true));
            root.Add(new XElement("requiredPreview", false));
            root.Add(new XElement("requiredModify", true));
            root.Add(new XElement("requiredAdd", true));
            root.Add(new XElement("pagesize", 20));
            XElement operato = new XElement("operato");
            XElement columns = new XElement("columns");
            for (int i = 0; i < cols.Length; i++)
            {
                XElement element = new XElement("column", new XAttribute("name", cols[i].ColName));
                if (cols[i].DbType.Equals("bit", StringComparison.CurrentCultureIgnoreCase))
                {
                    operato.Add(new XElement(
                        "item",
                        new XElement("text", "设置" + cols[i].Description),
                        new XElement("url", "list.aspx?actionType=set" + cols[i].ColName + "&value{!" + cols[i].ColName + "}")
                        ));
                }
                element.Add(new XElement("showInList", ("|ntext|text|image|binary|variant".IndexOf(cols[i].DbType) < 0) ? true : false));
                element.Add(new XElement("headText", cols[i].Description));
                element.Add(new XElement("searchCondition", true));
                element.Add(new XElement("columnForm", ColumnForm(cols[i])));
                element.Add(new XElement("allowAdd", !(cols[i].IsPrimaryKey || cols[i].IsDbGenerated)));
                element.Add(new XElement("allowModify", !(cols[i].IsPrimaryKey || cols[i].IsDbGenerated)));
                columns.Add(element);
            }
            root.Add(operato);
            root.Add(columns);
            root.Save(AppConfig.ManageSettingsRoot + table.Name + ".xml");

        }

        public static string ColumnForm(TableColumn tableCol)
        {
            if (tableCol.IsPrimaryKey)
            {
                return "InputHidden(" + tableCol.ColName + ")";
            }
            //如果过是外键约束的话，要用Select(ColumnName)
            switch (tableCol.DbType.ToLower())
            {
                case "bit":
                    return "InputBool(" + tableCol.ColName + ")";
                case "varchar":
                case "char":
                case "nvarchar":
                case "nchar":
                    if (tableCol.ColLength < 200)
                    {
                        return "InputText(" + tableCol.ColName + "," + (tableCol.ColLength / 4 < 15 ? 15 : tableCol.ColLength / 4) + ")";
                    }
                    else if (tableCol.ColLength < 500)
                    {
                        return "Textarea(" + tableCol.ColName + "," + (tableCol.ColLength / 4 < 15 ? 15 : tableCol.ColLength / 4) + ")";
                    }
                    else
                    {
                        return "FckEditor(" + tableCol.ColName + ",80%,300)";
                    }
                case "ntext":
                case "text":
                case "image":
                    return "FckEditor(" + tableCol.ColName + ",80%,300)";
                case "int":
                case "bigint":
                case "smallint":
                    return "InputText(" + tableCol.ColName + ",12)";
                case "long":
                case "float":
                    return "InputText(" + tableCol.ColName + ",12)";
                case "decimal":
                case "smallmoney":
                case "money":
                case "numeric":
                    return "InputText(" + tableCol.ColName + ",12)";
                case "datetime":
                    return "DateTimer(" + tableCol.ColName + ",20)";
                case "binary":
                case "varbinary":
                    return "InputText(" + tableCol.ColName + ",12)";
                case "uniqueidentifier":
                    return "InputText(" + tableCol.ColName + ",35)";
                case "variant":
                default:
                    return "InputText(" + tableCol.ColName + ",35)";
            }
        }
    }
}
